package org.nuplay;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.TargetDataLine;
import javax.sound.sampled.Mixer.Info;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;



public class NuPlayAudioAnalysisClient extends JFrame implements ActionListener, WindowListener, LineListener, ChangeListener {
	

	private JComboBox audio_devices_choose;
	private JTextField ip_number_choose;
	private JComboBox channel;
	
	private JButton connect_button;
	private boolean connected;
	private final static int amount_of_bins = 8;
	private FFT fft;
	private FFTCanvas canvas;
	private NuPlayConnector connector;

	
	public NuPlayAudioAnalysisClient() {
		connected = false;
		build_ui();
		init_audio_devices();
	}

	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource() == connect_button) {
			if (connected) {
				disconnect();
			} else {
				connect();
			}
		}
	}
	
	private void build_ui() {
		
		addWindowListener(this);
		getContentPane().setLayout( new BorderLayout() );

		
		
		
		JPanel topPanel = new JPanel();
		topPanel.setLayout( null );
		getContentPane().add( topPanel, BorderLayout.CENTER );
		
			JLabel label_ip_address = new JLabel("Where is NuPlay running at?");
		label_ip_address.setBounds(20,20, 200, 20);
		
		ip_number_choose = new JTextField();
		ip_number_choose.setBounds( 20, 40, 260, 20 );
		try {
			ip_number_choose.setText(InetAddress.getLocalHost().getHostAddress());
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		
		topPanel.add(label_ip_address);
		topPanel.add(ip_number_choose);
		
		
		JLabel channel_text = new JLabel("Which channel to use?");
		channel_text.setBounds(20,70, 200, 20);
	
		
		channel = new JComboBox();
		for(int i=0; i < 32;i++) {
			channel.addItem(new Integer(i + 1));
		}
		channel.setBounds( 20, 90, 250, 20 );
		topPanel.add(channel_text);
		topPanel.add(channel);
		
	
		
		canvas = new FFTCanvas();
		canvas.setBounds( 20, 130, 460, 200 );
		topPanel.add(canvas);
		
		
			

		
		
		
		connect_button = new JButton("Connect to NuPlay.");
		connect_button.addActionListener(this);
		connect_button.setBounds( 20, 250, 200, 20 );
		topPanel.add(connect_button);
	
		java.net.URL imageURL = NuPlayAudioAnalysisClient.class.getResource("nuplay.png");
		if (imageURL != null) {
			ImageIcon icon = new ImageIcon(imageURL);
			this.setIconImage(icon.getImage());
			JLabel image_label = new JLabel(icon);
			image_label.setBounds( 240, 210, 50, 77 );
			topPanel.add(image_label);
		}
	}
	
	public void update(LineEvent arg0) {
		// TODO Auto-generated method stub
		System.out.println("event" + arg0.getType());
	}
	
	private void init_audio_devices() {
		fft = new FFT(amount_of_bins, canvas);
	}
	
	private void close_audio_device() {
		fft.stop();
	}
	
	private void open_audio_device() {
		fft.start(connector);
	}
	
	
	private void disconnect() {
		connected = false;
		connect_button.setText("Connect to NuPlay.");
		close_audio_device();
		if (connector != null) {
			connector.close();
			connector = null;
		}
	}
	
	private void connect() {
		connected = true;
		connect_button.setText("Disconnect from NuPlay.");
		if (connector != null) {
			connector.close();
		}
		try {
			connector = new NuPlayConnector(InetAddress.getByName(ip_number_choose.getText()),(Integer)channel.getSelectedItem() );
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		open_audio_device();
	}
	
	public void close() {
		System.exit(0);
	}

	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		JFrame main_window = new NuPlayAudioAnalysisClient();
		main_window.setSize(310, 320);
		main_window.setTitle("NuPlay AUDIO-analysis client V1.0");
		main_window.setVisible(true);
	}

	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void windowClosing(WindowEvent e) {
		close();
		
	}

	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void stateChanged(ChangeEvent e) {
		// TODO Auto-generated method stub
	}


	
}
